package com.sktlab.bizconfmobile.model.manager;

import java.util.ArrayList;
import java.util.HashMap;

import com.sktlab.bizconfmobile.model.Conference;
import com.sktlab.bizconfmobile.model.ConferenceAccount;
import com.sktlab.bizconfmobile.model.Participant;
import com.sktlab.bizconfmobile.util.Util;

public class CommunicationManager {

	public static final String TAG = "CommunicationManager";
	
	private ConferenceAccount mActiveAccount;
	//private Conference mActiveConference;

	// used to store the active conference in current bridge
	private HashMap<String, Conference> mActiveConfs;
	
	/**
	 * 
	 * This store the participants of the conference which is going now,
	 * this is mostly used when user join a conference, he is not the moderator,
	 * so he didn't know how many people join this conference, we can use this data structure
	 * to get those people.
	 */
	private HashMap<String, Participant> mActiveParties;
	
	/**
	 * This data structure store the party id which had been outcalled by the moderator,
	 * this mostly used in start a conference, so the moderator can add participant to a 
	 * conference, when add a participant, if we out call it default.
	 */
	private ArrayList<Participant> outCallPartys;
	
	private static class InstanceHolder {

		private final static CommunicationManager instance = new CommunicationManager();
	}

	private CommunicationManager() {
		
		mActiveConfs = new HashMap<String, Conference>();
		outCallPartys = new ArrayList<Participant>();
		mActiveParties = new HashMap<String, Participant>();
	}
	
	public static CommunicationManager getInstance() {

		return InstanceHolder.instance;
	}
	
	public HashMap<String, Participant> getActiveParties() {
		
		return mActiveParties;
	}
	
	public void putParty(Participant party) {
		
		do {
			
			if (Util.isEmpty(party)) {
				
				break;
			}
			
			if (mActiveParties.containsKey(party.getIdInConference())) {
				
				break;
			}
			
			mActiveParties.put(party.getIdInConference(), party);
		}while(false);		
	}
	
	public ConferenceAccount getActiveAccount() {
		return mActiveAccount;
	}

	public void setActiveAccount(ConferenceAccount mActiveAccount) {
		this.mActiveAccount = mActiveAccount;
	}

	public Conference getActiveConfByKey(String key) {

		if (mActiveConfs.containsKey(key)) {

			return mActiveConfs.get(key);
		}

		return null;
	}

	public void putActiveConference(Conference conf) {

		if (!Util.isEmpty(conf)) {

			String key = conf.getAttr().getConfName();

			Util.BIZ_CONF_DEBUG(TAG, "active conf key: " + key);

			if (!Util.isEmpty(key) && !mActiveConfs.containsKey(key)) {

				mActiveConfs.put(key, conf);
			} else {

				Util.BIZ_CONF_DEBUG(TAG,
						"active conference's conf Id is null or had been in map");
			}

		} else {

			Util.BIZ_CONF_DEBUG(TAG, "active conference is null");
		}
	}

	public HashMap<String, Conference> getActiveConfs() {
		return mActiveConfs;
	}

	public void setActiveConfs(HashMap<String, Conference> mActiveConfs) {

		this.mActiveConfs = mActiveConfs;
	}

	public void clearActiveConfs() {

		mActiveConfs.clear();
	}

	public void addOutCallParty(Participant party) {

		outCallPartys.add(party);
	}

	public ArrayList<Participant> getOutCallPartys() {
		return outCallPartys;
	}

	public void setOutCallPartyIds(ArrayList<Participant> outCallPartys) {
		this.outCallPartys = outCallPartys;
	}
	
	public void reset() {
		
		outCallPartys.clear();
		mActiveConfs.clear();
		mActiveParties.clear();
	}
}
